/*
 * Copyright (C) 2003, Velocityme Partnership
 * PermissionBean.java
 *
 * Created on 21 April 2003, 07:48
 */

package com.velocityme.entity;

import javax.ejb.*;
import javax.naming.*;
import java.rmi.*;
import javax.rmi.*;
import com.velocityme.interfaces.*;
import com.velocityme.interfaces.ServiceUnavailableException;

/**
 *
 * @author  Robert Crida Work
 * @ejb.bean
 *           type="CMP"
 *           cmp-version="2.x"
 *           name="Permission"
 *           jndi-name="ejb/Permission"
 *           view-type="local"
 * @ejb.transaction type="Required"
 *
 * @ejb.value-object match="*"
 *
 * @ejb.persistence table-name="permission"
 *
 * @ejb.util generate="physical"
 *
 * @ejb.finder signature="java.util.Collection findAll()"
 *              query="SELECT OBJECT(o) FROM Permission o"
 *
 * @ejb.finder signature="com.velocityme.interfaces.Permission findByName(java.lang.String name)"
 *              query="SELECT OBJECT(o) FROM Permission o WHERE o.name = ?1"
 *
 * @ejb.finder signature="java.util.Collection findByUserAndSearch(com.velocityme.interfaces.UserLocal user, com.velocityme.interfaces.SearchLocal search, com.velocityme.interfaces.NodeLocal node)"
 *              query="SELECT DISTINCT OBJECT(p) FROM Search s, IN (s.searchLevelsLocal) l, IN (l.nodesLocal) n, UserAccess ua, Role r, IN (r.permissionsLocal) p WHERE (ua.userLocal = ?1 AND ua.roleLocal = r AND ((s = ?2 AND r.isRecursive = TRUE AND ua.nodeLocal = n) OR (ua.nodeLocal = ?3 AND r.isRecursive = FALSE)))"
// *              query="SELECT DISTINCT OBJECT(p) FROM Search s, IN (s.searchLevelsLocal) l, UserAccess u, Role r, IN (r.permissionsLocal) p WHERE u.userLocal = ?1 AND u.roleLocal = r AND ((s = ?2 AND r.isRecursive = TRUE AND u.nodeLocal MEMBER OF l.nodesLocal) OR (u.nodeLocal = ?3 AND r.isRecursive = FALSE))"
 *
 * @ejb.finder signature="java.util.Collection findByGroupAndSearch(com.velocityme.interfaces.UserLocal user, com.velocityme.interfaces.SearchLocal search, com.velocityme.interfaces.NodeLocal node)"
 *              query="SELECT DISTINCT OBJECT(p) FROM Search s, IN (s.searchLevelsLocal) l, IN (l.nodesLocal) n, GroupAccess ga, IN (ga.groupLocal.usersLocal) gu, Role r, IN (r.permissionsLocal) p WHERE (gu = ?1 AND ga.roleLocal = r AND ((s = ?2 AND r.isRecursive = TRUE AND ga.nodeLocal = n) OR (ga.nodeLocal = ?3 AND r.isRecursive = FALSE)))"
 *
/* @jboss.persistence create-table="true"
 *                    remove-table="false"
 */
public abstract class PermissionBean implements EntityBean {
    
    public static final int NODE_CREATE = 0;
    public static final int NODE_EDIT = 1;
    public static final int NODE_DELETE = 2;
    public static final int NODE_POST_COMMENT = 3;
    public static final int TASK_CHANGE_STATE = 4;
    public static final int ACTION_CREATE = 5;
    public static final int ACTION_EDIT = 6;
    public static final int PROJECT_CREATE = 7;
    public static final int PROJECT_EDIT = 8;
    public static final int ISSUE_CREATE = 9;
    public static final int ISSUE_EDIT = 10;
    public static final int NOTE_CREATE = 11;
    public static final int NOTE_EDIT = 12;
    public static final int DEPARTMENT_CREATE = 13;
    public static final int DEPARTMENT_EDIT = 14;
    public static final int ORGANISATION_CREATE = 15;
    public static final int ORGANISATION_EDIT = 16;
    public static final int PERSON_CREATE = 17;
    public static final int PERSON_EDIT = 18;
    public static final int USER_CREATE = 19;
    public static final int USER_EDIT = 20;
    public static final int USER_CHANGE_PASSWORD = 21;
    public static final int GROUP_CREATE = 22;
    public static final int GROUP_EDIT = 23;
    public static final int CONTACT_DETAIL_TYPE_EDIT = 24;
    public static final int TASK_TYPE_CREATE = 25;
    public static final int TASK_TYPE_EDIT = 26;
    public static final int STATE_CREATE = 27;
    public static final int STATE_EDIT = 28;
    public static final int STATUS_CREATE = 29;
    public static final int STATUS_EDIT = 30;
    public static final int ROLE_CREATE = 31;
    public static final int ROLE_EDIT = 32;
    public static final int USER_ACCESS_CREATE = 33;
    public static final int USER_ACCESS_DELETE = 34;
    public static final int GROUP_ACCESS_CREATE = 35;
    public static final int GROUP_ACCESS_DELETE = 36;
    public static final int TIME_CREATE = 37;
    public static final int TIME_EDIT = 38;
    public static final int TIME_DELETE = 39;
    public static final int USER_ENABLE = 40;
    public static final int NODE_VIEW = 41;
    public static final int SYSTEM_PROPERTY_EDIT = 42;
    public static final int ANCHOR_CREATE = 43;
    public static final int ANCHOR_EDIT = 44;
    public static final int TASK_REASSIGN_RESPONSIBILITY = 45;
    public static final int TASK_EDIT_DEPENDENCY = 46;
    public static final int SPRINT_CREATE = 47;
    public static final int SPRINT_EDIT = 48;
    public static final int NUM_TYPES = 49;
  
    /**
    * Context set by container
    */
    private javax.ejb.EntityContext m_entityContext;

    public void setEntityContext(javax.ejb.EntityContext entityContext) {
        m_entityContext = entityContext;
    }

    public void unsetEntityContext() {
        m_entityContext = null;
    }
  
    /** @ejb.create-method */
    public PermissionPK ejbCreate(java.lang.Integer p_type) throws javax.ejb.CreateException {
        setPermissionId(p_type);
        // EJB 2.0 spec says return null for CMP ejbCreate methods.
        return null;
    }

    public void ejbPostCreate(java.lang.Integer p_type) throws javax.ejb.CreateException {
        // CMR relations must be set in ejbPostCreate
        String name;
        switch(p_type.intValue()) {
            case NODE_CREATE:                   name = "Node Create"; break;
            case NODE_EDIT:                     name = "Node Edit"; break;
            case NODE_DELETE:                   name = "Node Delete"; break;
            case NODE_POST_COMMENT:             name = "Node Post Comment"; break;
            case TASK_CHANGE_STATE:             name = "Task Change State"; break;
            case ACTION_CREATE:                 name = "Action Create"; break;
            case ACTION_EDIT:                   name = "Action Edit"; break;
            case PROJECT_CREATE:                name = "Project Create"; break;
            case PROJECT_EDIT:                  name = "Project Edit"; break;
            case ISSUE_CREATE:                  name = "Issue Create"; break;
            case ISSUE_EDIT:                    name = "Issue Edit"; break;
            case DEPARTMENT_CREATE:             name = "Department Create"; break;
            case DEPARTMENT_EDIT:               name = "Department Edit"; break;
            case ORGANISATION_CREATE:           name = "Organisation Create"; break;
            case ORGANISATION_EDIT:             name = "Organisation Edit"; break;
            case PERSON_CREATE:                 name = "Person Create"; break;
            case PERSON_EDIT:                   name = "Person Edit"; break;
            case USER_CREATE:                   name = "User Create"; break;
            case USER_EDIT:                     name = "User Edit"; break;
            case USER_CHANGE_PASSWORD:          name = "User Change Password"; break;
            case GROUP_CREATE:                  name = "Group Create"; break;
            case GROUP_EDIT:                    name = "Group Edit"; break;
            case CONTACT_DETAIL_TYPE_EDIT:      name = "Contact Detail Type Edit"; break;
            case TASK_TYPE_CREATE:              name = "Task Type Create"; break;
            case TASK_TYPE_EDIT:                name = "Task Type Edit"; break;
            case STATE_CREATE:                  name = "State Create"; break;
            case STATE_EDIT:                    name = "State Edit"; break;
            case STATUS_CREATE:                 name = "Status Create"; break;
            case STATUS_EDIT:                   name = "Status Edit"; break;
            case ROLE_CREATE:                   name = "Role Create"; break;
            case ROLE_EDIT:                     name = "Role Edit"; break;
            case USER_ACCESS_CREATE:            name = "User Access Create"; break;
            case USER_ACCESS_DELETE:            name = "User Access Delete"; break;
            case GROUP_ACCESS_CREATE:           name = "Group Access Create"; break;
            case GROUP_ACCESS_DELETE:           name = "Group Access Delete"; break;
            case TIME_CREATE:                   name = "Time Create"; break;
            case TIME_EDIT:                     name = "Time Edit"; break;
            case TIME_DELETE:                   name = "Time Delete"; break;
            case USER_ENABLE:                   name = "User Enable"; break;
            case NODE_VIEW:                     name = "Node View"; break;
            case SYSTEM_PROPERTY_EDIT:          name = "System Property Edit"; break;
            case ANCHOR_CREATE:                 name = "Anchor Create"; break;
            case ANCHOR_EDIT:                   name = "Anchor Edit"; break;
            case TASK_REASSIGN_RESPONSIBILITY:  name = "Task Reassign Responsibility"; break;
            case TASK_EDIT_DEPENDENCY:          name = "Task Edit Dependency"; break;
            case SPRINT_CREATE:                 name = "Sprint Create"; break;
            case SPRINT_EDIT:                   name = "Sprint Edit"; break;
            default:                            name = "Unknown Permission"; break;
        }
        setName(name);
    }
    
    /**
     *  This gets a local object from a remote object.
     *  @ejb.interface-method view-type="remote"
     */
    public PermissionLocal getLocal() {
        return (PermissionLocal)m_entityContext.getEJBLocalObject();
    }
    
    /** @ejb.interface-method view-type="local" */
    public abstract com.velocityme.valueobjects.PermissionValue getPermissionValue();

    //=======================================================================
    //
    //                   ATTRIBUTES
    //
    //=======================================================================
      
    /**
     * @ejb.persistence column-name="permissionId"
     * @ejb.interface-method view-type="local"
     * @ejb.pk-field 
     */
    public abstract java.lang.Integer getPermissionId();
    public abstract void setPermissionId(java.lang.Integer permissionId);
    
    /**
     * @ejb.persistence column-name="name"
     * @ejb.interface-method view-type="local"
     */
    public abstract java.lang.String getName();
    public abstract void setName(java.lang.String name);
    
    //=======================================================================
    //
    //                   RELATIONSHIPS
    //
    //=======================================================================
    
    /**
     * @ejb.interface-method view-type="local"
     * @ejb.relation name="Role-Permission"
     *               role-name="Permission-has-many-Roles"
     * @jboss.relation fk-column="permissionId"
     *                 related-pk-field="roleId"
     * @jboss.relation-table table-name="rolepermission"
     *                       create-table="true"
     *                       remove-table="false"
     */
    public abstract java.util.Collection getRolesLocal();
    /** @ejb.interface-method view-type="local" */
    public abstract void setRolesLocal(java.util.Collection roles);
    
    //=======================================================================
    //
    //                   MANAGEMENT METHODS
    //
    //=======================================================================
    
}
